package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.dao.model.ProductMethodDO;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Believe
 * Date 2022/6/7 10:54
 */
public class ProductMethodFilter {
    public static List<ProductMethodDO> toUpdateList(List<ProductMethodDO> productMethodDOList, List<ProductMethodDO> queryMethodList) {
        List<ProductMethodDO> methodDOList = Lists.newLinkedList();
        Map<Long, List<ProductMethodDO>> listMap = new HashMap<>();
        for (ProductMethodDO productMethodDO : queryMethodList) {
            if (listMap.containsKey(productMethodDO.getId())) {
                List<ProductMethodDO> methodDOS = listMap.get(productMethodDO.getId());
                methodDOS.add(productMethodDO);
            } else {
                List<ProductMethodDO> methodDOS = new LinkedList<>();
                methodDOS.add(productMethodDO);
                listMap.put(productMethodDO.getId(), methodDOS);
            }
        }
        for (ProductMethodDO productMethodDO : productMethodDOList) {
            if (listMap.containsKey(productMethodDO.getId())) {
                methodDOList.add(productMethodDO);
            }
        }
        return methodDOList;
    }

    public static List<ProductMethodDO> toInsertList(List<ProductMethodDO> productMethodDOList, List<ProductMethodDO> queryMethodList) {
        List<ProductMethodDO> methodDOList = Lists.newLinkedList();
        Map<Long, List<ProductMethodDO>> listMap = new HashMap<>();
        for (ProductMethodDO productMethodDO : queryMethodList) {
            if (listMap.containsKey(productMethodDO.getId())) {
                List<ProductMethodDO> methodDOS = listMap.get(productMethodDO.getId());
                methodDOS.add(productMethodDO);
            } else {
                List<ProductMethodDO> methodDOS = new LinkedList<>();
                methodDOS.add(productMethodDO);
                listMap.put(productMethodDO.getId(), methodDOS);
            }
        }
        for (ProductMethodDO productMethodDO : productMethodDOList) {
            if (!listMap.containsKey(productMethodDO.getId())) {
                methodDOList.add(productMethodDO);
            }
        }
        return methodDOList;
    }
}
